from urllib import request, parse
from lxml import etree
import ssl
import re

# 取消 https 代理验证
ssl._create_default_https_context = ssl._create_unverified_context


def dyldclassfy(self, line, search):
    # 报头
    # user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36'
    user_agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
    headers = {'User-Agent': user_agent}
    url = ''
    dyld_url = ''
    if line == '1':
        dyld_url = "http://dianyingleida.com"
        url = dyld_url + "/search.php?searchtype=5&" + search
    elif line == '2':
        dyld_url = "http://www.nmkan.com"
        url = dyld_url + "/search.php?searchtype=5&" + search
    req = request.Request(url, headers=headers)
    try:
        response = request.urlopen(req)
        # 获取每页的HTML源码字符串
        html = response.read().decode('utf-8')
        # 解析html 为 HTML 文档
        selector = etree.HTML(html)
        # 获取上部轮播列表信息
        # slider_list = selector.xpath('//div[@class="flickity clearfix"]//div[@class="myui-vodlist__box"]')
        slider_list = selector.xpath('//div[contains(@class,"flickity")]//div[@class="myui-vodlist__box"]/a')
        slider_node_list = []
        for node in slider_list:
            title = node.xpath('@title')[0]
            style = node.xpath('@style')[0]
            poster = re.findall('(?<=url\().*?(?=\))', style)[0]
            if poster.startswith('/upload'):
                poster = dyld_url + poster
            link = dyld_url + node.xpath('@href')[0]
            fen = node.xpath('span[@class="pic-tag pic-tag-top"]/text()')[0]
            defalut_result = {
                "title": title,
                "poster": poster,
                "link": link,
                "fen": fen,
            }
            slider_node_list.append(defalut_result)

        # 分类的视频
        classify_list = selector.xpath('//ul[@class="myui-vodlist clearfix"]/li/div')
        classify_node_list = []
        for node in classify_list:
            title = node.xpath('a[@class="myui-vodlist__thumb lazyload"]/@title')[0]
            poster = node.xpath('a[@class="myui-vodlist__thumb lazyload"]/@data-original')[0]
            if poster.startswith('/upload'):
                poster = dyld_url + poster
            link = dyld_url + node.xpath('a[@class="myui-vodlist__thumb lazyload"]/@href')[0]
            fen = node.xpath('a[@class="myui-vodlist__thumb lazyload"]/span[@class="pic-tag pic-tag-top"]/text()')[0]
            definition = \
            node.xpath('a[@class="myui-vodlist__thumb lazyload"]//span[@class="pic-text text-right"]/text()')[0]
            intro = node.xpath('div[@class="myui-vodlist__detail"]/p/text()')[0]
            defalut_result = {
                "title": title,
                "poster": poster,
                "link": link,
                "fen": fen,
                "definition": definition,
                "intro": intro,
            }
            classify_node_list.append(defalut_result)

        # 总页数
        total = selector.xpath('//ul[@class="myui-page text-center clearfix"]/li[last()-2]/a/text()')[0].split('/')[1]
        result = {
            "slider_list": slider_node_list,
            "classify_list": classify_node_list,
            "total": total,
        }

        return result
    except Exception:
        print(url, Exception)
        data = {"code": -1, "message": "error", "data": None}
        return data
